<?php
$servername = "数据库地址";
$username = "用户名";
$password = "密码";
 
 //返回数据对象
 $response = array();
 $res = array('res'=>false);
 
// 创建连接
$conn = new mysqli($servername, $username, $password, "数据库名");
 
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}else{
	
	$action = "NULL";
	
	if(isset($_POST['submit_message'])){
		$action  = $_POST['submit_message'];
	}
	
	//请求登录
	if($action=="login")
	{
		if(isset($_POST['loginName']) == false || isset($_POST['password']) == false){
			//用户名和密码为空！
			$response['result'] = '0';
			$response['reason'] = '服务器未能收到用户名和密码！';
		}else{
			$conn -> set_charset('UTF-8');             // 设置数据库字符集
			$sql = "select * from user where id='1'";     // SQL 语句
			$result = $conn -> query($sql);            // 执行上面的 SQL 语句
	
			$data = $result -> fetch_all();
		
			$loginName = $data[0][1];
			$password = $data[0][2];
			if($loginName == $_POST['loginName'] && $password == $_POST['password']){
				//密码正确！
				$response['result'] = '1';
				$response['info'] = $data[0];
			}else{
				//密码错误
				$response['result'] = '0';
				$response['reason'] = '密码或用户名错误！';
			}
		}	
	}
	
	//获取个人简介
	if($action=="getIntroduction"){
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "select introduction from user where id='1'";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
			
		$data = $result -> fetch_all();
				
		$response['result'] = '1';
		$response['info'] = $data[0];
	}
	
	//修改个人简介
	if($action=="modifyIntroduction"){
		$introduction = $_POST['introduction'];
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "update user set introduction='$introduction' where id='1'";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
		
		if($result){
			$response['result'] = '1';
			$response['info'] = $introduction;
		}else{
			$response['result'] = '0';
			$response['reason'] = '更新失败了/(ㄒoㄒ)/~~';
		}	
	}
	
	//获取pageList
	if($action=="getPageList"){
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "select * from page";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
			
		$data = $result -> fetch_all();
				
		$response['result'] = '1';
		$response['info'] = $data;
	}
	
	//删除某一网址
	if($action=="deletePage"){
		$id = $_POST['id'];
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "delete from page where id='$id'";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
		
		if($result){
			$response['result'] = '1';
		}else{
			$response['result'] = '0';
			$response['reason'] = '服务器出错！';
		}	
	}
	//获取某一网址信息
	if($action=="getPageById"){
		$id = $_POST['id'];
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "select name,url from page where id='$id'";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
		
		$data = $result -> fetch_all();
				
		$response['result'] = '1';
		$response['info'] = $data;
	}
	
	//修改某一网址信息
	if($action=="modifyPage"){
		$id = $_POST['id'];
		$pageName = $_POST['pageName'];
		$url = $_POST['url'];
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "update page set name='$pageName',url='$url' where id='$id'";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
		
		if($result){
			$response['result'] = '1';
		}else{
			$response['result'] = '0';
			$response['reason'] = '服务器出错！';
		}	
	}
	
	//增加page
	if($action=="addPage"){
		$pageName = $_POST['pageName'];
		$url = $_POST['url'];
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "insert into page(name,url) values('$pageName','$url')";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
		
		if($result){
			$response['result'] = '1';
		}else{
			$response['result'] = '0';
			$response['reason'] = '服务器出错！';
		}	
	}
	
	
	//获取msgList
	if($action=="getMessageList"){
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "select * from message order by time desc";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
			
		$data = $result -> fetch_all();
				
		$response['result'] = '1';
		$response['info'] = $data;
	}
	
	//删除一条msg
	if($action=="deleteMsg"){
		$id = $_POST['id'];
		$conn -> set_charset('UTF-8');             // 设置数据库字符集
		$sql = "delete from message where id='$id'";     // SQL 语句
		$result = $conn -> query($sql);            // 执行上面的 SQL 语句
		
		if($result){
			$response['result'] = '1';
		}else{
			$response['result'] = '0';
			$response['reason'] = '服务器出错！';
		}	
	}
	
	$conn -> close();
	
}

echo json_encode($response);
?>